<?php
/*
 * Created on Oct 25, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
date_default_timezone_set("Asia/Shanghai");

function multiwordbreak($str,$maxlen = 33){
	$len = mb_strlen($str, 'UTF-8');
	$ch = '';
	if($len<=$maxlen) return $str;
	for ($i=0; $i < $len; $i++){
		if($i%$maxlen==0){
			  $ch .= mb_substr($str, $i, $maxlen, 'UTF-8')."<br />";
		}
    }
    return $ch;
}
function break_word($matches){

	return $matches[1].multiwordbreak($matches[3],$matches[2]);
}

require_once('html2fpdf.php');
$pdf = new HTML2FPDF("P","mm");

$data = isset($_POST['data'])?$_POST['data']:'';

$filename = 'spot_data'.rand(1,2000).'.pdf';

$strContent = '';
	$c = preg_replace_callback('/(.*?)<span\s*id="br_(\d+)">(.*?)<\/span>(.*?)/ism',"break_word",$data);

	if(strpos($c,'border="0"')!==false) $c = str_replace('border="0"','border="1"',$c);

	$strContent = stripslashes($c);

$strContent = str_replace("\r\n","&lt;br /&gt;",$strContent);

$pdf->SetMargins(10,20,-1);//设置左右及上边距
$pdf->AddGBFont();
$pdf->AddPage();

$pdf->WriteHTML($strContent);
//$pdf->WriteHTML($strContent);
//$pdf->Output('../tmp/spot_data.pdf');
$pdf->Output('../public/upload/'.$filename);

$d['status'] = "OK";
$d['fname'] = $filename;
echo json_encode($d);
?>
